/****************************** Module Header ******************************\
 * Module Name:  RegExp.cs
 * Module Version:  1.0.0 (2011-9-28)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to regularexpressions validator.  It supplies 
 * most validator ruler define.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Yule8xCommon.Applib
{
    /// <summary>
    /// 正则表达式匹配类
    /// <para>提供了手机号、电子邮件、网址、IP地址、用户名、密码、电话号码、手机号码、QQ号码、HTML标签、中文字符等的验证规则</para>
    /// </summary>
    public class RegularUtil
    {
        /// <summary>
        /// 正则匹配规则表
        /// </summary>
        public struct Rules
        {
            /// <summary>
            /// 匹配手机号码
            /// </summary>
            public static string MobileNumber { get { return @"^1\d{10}$"; } }

            /// <summary>
            /// 匹配Email地址
            /// </summary>
            public static string Email { get { return @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; } }

            /// <summary>
            /// 匹配身份证,中国的身份证为15位或18位
            /// </summary>
            public static string IdentityCardNumber { get { return @"\d{15}|\d{18}"; } }

            /// <summary>
            /// 匹配网址URL
            /// </summary>
            public static string URL { get { return @"[a-zA-z]+://[^\s]*"; } }

            /// <summary>
            /// 匹配ip地址
            /// </summary>
            public static string IP { get { return @"\d+\.\d+\.\d+\.\d+"; } }

            /// <summary>
            /// 匹配帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)
            /// </summary>
            public static string UserName { get { return @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$"; } }

            /// <summary>
            /// 匹配用户密码(字母开头，长度在6~18之间，只能包含字符、数字和下划线)
            /// </summary>
            public static string Password { get { return @"^[a-zA-Z]\w{5,17}$"; } }

            /// <summary>
            /// 匹配国内电话号码,匹配形式如 0511-4405222 或 021-87888822
            /// </summary>
            public static string TelphoneNumber { get { return @"\d{3}-\d{8}|\d{4}-\d{7}"; } }

            /// <summary>
            /// 匹配腾讯QQ号,腾讯QQ号从10000开始
            /// </summary>
            public static string QQNumber { get { return @"[1-9][0-9]{4,}"; } }

            /// <summary>
            /// 匹配中国邮政编码,中国邮政编码为6位数字
            /// </summary>
            public static string PostCode { get { return @"[1-9]\d{5}(?!\d)"; } }

            /// <summary>
            /// 匹配中文字符
            /// </summary>
            public static string ChineseCharacter { get { return @"[\u4e00-\u9fa5]"; } }

            /// <summary>
            /// 匹配HTML标记
            /// </summary>
            public static string HTMLTag { get { return @"<(\S*?)[^>]*>.*?</\1>|<.*? />"; } }

            /// <summary>
            /// 匹配数字
            /// </summary>
            public static string Number { get { return @"^\d*$"; } }

            /// <summary>
            /// 匹配小数
            /// </summary>
            public static string Decimal { get { return "[0].d{1,2}|[1]"; } }

            /// <summary>
            /// 匹配中国联通移动电话号码,2G号段(GSM)：130、131、132、155、156；3G号段(WCDMA，沃-WO)：185、186
            /// </summary>
            public static string ChinaUnicom { get { return @"^(130|131|132|155|156|185|186)\d{8}$"; } }

            /// <summary>
            /// 匹配中国移动移动电话号码,2G号段(GSM)：134、135、136、137、138、139、150、151、152、158、159；3G号段(TD-SCDMA，G3)：157、187、188、147(上网本)
            /// </summary>
            public static string ChinaMobile { get { return @"^(134|135|136|137|138|139|147|150|151|152|157|158|159|182|187|188)\d{8}$"; } }

            /// <summary>
            /// 匹配中国电信移动电话号码,2G号段(CDMA)：133、153；3G号段(CDMA2000，天翼)：180、189
            /// </summary>
            public static string ChinaTeleCom { get { return @"^(133|153|180|189)\d{8}$"; } }

            /// <summary>
            /// 图片等路径
            /// </summary>
            public static string Src { get { return @"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>"; ; } }
        }

        /// <summary>
        /// 检测是否匹配正则表达式的内容
        /// </summary>
        /// <param name="Text">要验证的字符串</param>
        /// <param name="Pattern">正则表达式模式</param>
        /// <returns></returns>
        public static bool IsMatch(string Text, string Pattern)
        {
            return Regex.IsMatch(Text, Pattern);
        }
        /// <summary>
        /// 替换匹配正则的内容
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="Pattern"></param>
        /// <param name="replaceMent"></param>
        /// <returns></returns>
        public static string Replace(string Text, string Pattern, string replaceMent)
        {
            return Regex.Replace(Text, Pattern, replaceMent);
        }
        /// <summary>
        /// 拆分字符串
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static string[] Split(string Text, string Pattern)
        {
            Regex reg = new Regex(Pattern);
            MatchCollection mc = reg.Matches(Text);
            string[] arrOut = new string[mc.Count];
            int i = 0;
            foreach (Match m in mc)
            {
                arrOut[i] = m.ToString();
                i++;
            }
            return arrOut;
        }
        /// <summary>
        /// 查找内容
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static IList<String> Match(string text, string pattern)
        {
            IList<String> list = new List<String>();
            if (string.IsNullOrEmpty(text)) return list;
            foreach (Match m in Regex.Matches(text, pattern, RegexOptions.IgnoreCase))
            {
                list.Add(m.Value);
            }
            return list;
        }

        public static string GetImgUrl(string text)
        {
            StringBuilder str = new StringBuilder();
            string pat = @"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>";
            Regex r = new Regex(pat, RegexOptions.Compiled);
            Match m = r.Match(text.ToLower());
            while (m.Success)
            {
                Group g = m.Groups[2];
                str.Append(g).Append(",");
                m = m.NextMatch();
            }
            return str.Replace("\"", "").ToString().TrimEnd(',');
        }

    }
}
